//
//  HJGetOnViewController.m
//  YaDiuDiu
//
//  Created by 胡光健 on 2017/10/28.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "HJGetOnViewController.h"
#import "HJHeader.h"
#import "MessViewController.h"
#import "ShopCenterCell.h"
#import "HJGetOnModel.h"
#import "HJOderViewController.h"
#import "YueViewController.h"
#import "PingJiaViewController.h"
#import "DPZLViewController.h"
#import "KFuViewController.h"
@interface HJGetOnViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView * tableView;
@property(nonatomic,strong)HJHeader *header;
@property(nonatomic,strong)NSArray * titleAr;
@property(nonatomic,strong)NSArray * imageAr;
@property(nonatomic,strong)HJGetOnModel * getonModel;
@end

@implementation HJGetOnViewController
-(NSArray *)titleAr {
    if (!_titleAr) {
        _titleAr = @[@[@"我的余额",@"店铺评论"],@[@"店铺资料"],@[@"推广用户",@"推广骑手"],@[@"客服中心"]];
    }
    return _titleAr;
}

-(NSArray *)imageAr {
    if (!_imageAr) {
        _imageAr = @[@[@"wdye",@"评论-1"],@[@"053-店铺资料-拷贝"],@[@"用户",@"问题-骑手问题"],@[@"客服"]];
    }
    return _imageAr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadNav];
    [self configTableView];
    [self loadDataWork];
}
- (void)loadNav{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 64)];
    view.backgroundColor = [UIColor whiteColor];
    view.userInteractionEnabled = YES;
    [self.view addSubview:view];
    
    UILabel *lab = [[UILabel alloc] init];
    lab.text = @"商家中心";
    lab.textColor = [UIColor blackColor];
    lab.font = [UIFont systemFontOfSize:18.0];
    [view addSubview:lab];
    [lab makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(view.centerX);
        make.top.equalTo(34);
    }];
    
    UILabel *line = [[UILabel alloc] init];
    line.backgroundColor = [UIColor lightGrayColor];
    [view addSubview:line];
    [line makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(0);
        make.right.equalTo(0);
        make.bottom.equalTo(view.bottom);
        make.height.equalTo(0.5);
    }];
    
    //返回按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateHighlighted];
    [btn addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    [btn makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lab.centerY);
        make.left.equalTo(5);
        make.width.equalTo(30);
        make.height.equalTo(30);
    }];
    
    //按钮
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn2 setImage:[UIImage imageNamed:@"消息2"] forState:UIControlStateNormal];
    [btn2 setImage:[UIImage imageNamed:@"消息2"] forState:UIControlStateHighlighted];
    [btn2 addTarget:self action:@selector(clickRight) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn2];
    [btn2 makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lab.centerY);
        make.right.equalTo(-5);
        make.width.equalTo(30);
        make.height.equalTo(30);
    }];
    
}
// 返回
- (void)clickBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)clickRight{
    MessViewController *mess = [[MessViewController alloc] init];
    [self.navigationController pushViewController:mess animated:YES];
}

-(void)configTableView {
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.tableHeaderView = [self configHeader];
    [self.tableView registerCellByNibName:@"ShopCenterCell"];
    [self.view addSubview:self.tableView];
}
-(UIView *)configHeader {
    self.header = [[HJHeader alloc]init];
    self.header.frame = CGRectMake(0, 0, ScreenWidth, 300);
    WeakSelf
    self.header.oderBtnBlock = ^(NSInteger tag,NSString * merchantid) {
        HJOderViewController * oderVC = [[HJOderViewController alloc]init];
        if (tag==200) {
            oderVC.type =stayType;
            oderVC.navTitle = @"待接单";
        }else if (tag==201) {
            oderVC.type = alreadyType;
            oderVC.navTitle = @"已接单";
        }else {
            oderVC.type = alreadyType;
            oderVC.navTitle = @"已完成订单";
        }
        oderVC.merchantid = merchantid;
        [weakSelf.navigationController pushViewController:oderVC animated:YES];
    };
    
    return self.header;
    
}

-(void)loadDataWork {
    NSString * url = [NSString stringWithFormat:@"%@%@",urlHead,@"/App/Merchantcentre/onthedoor/userid"];
    
    [TZHttpTool get:url params:@{@"userid":[user objectForKey:@"userid"]} success:^(NSDictionary *result) {
        [LICGToast showWithText:@"加载成功"];
        self.getonModel = [[HJGetOnModel alloc]init];
        self.getonModel = [HJGetOnModel mj_objectWithKeyValues:result[@"result"]];
        self.header.model = self.getonModel;
        [self.tableView reloadData];
    } failure:^(NSString *error) {
        [LICGToast showWithText:error];
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.titleAr[section] count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ShopCenterCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ShopCenterCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section==2) {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }else {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    cell.titleLabel.text = self.titleAr[indexPath.section][indexPath.row];
    cell.avater.image = [UIImage imageNamed:self.imageAr[indexPath.section][indexPath.row]];
    if (indexPath.section==0) {
        if (indexPath.row==0) {
            cell.subTitleLabel.text = [NSString stringWithFormat:@"%@元",self.getonModel.usermoney];
        }else {
            cell.subTitleLabel.text = self.getonModel.shopcomment;
        }
    }else if (indexPath.section==2) {
        if (indexPath.row==0) {
            cell.subTitleLabel.text = [NSString stringWithFormat:@"%@人",self.getonModel.tgcountuser];
        }else {
            cell.subTitleLabel.text = [NSString stringWithFormat:@"%@人",self.getonModel.horseman];
        }
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section==0) {
        if (indexPath.row==0) {
            YueViewController * vc = [[YueViewController alloc]init];
            vc.price = self.getonModel.totalmoney;
            [self.navigationController pushViewController:vc animated:YES];
        }else {
            PingJiaViewController * pingjia = [[PingJiaViewController alloc]init];
            pingjia.merid = self.getonModel.merchant.merchant_id;
            pingjia.url = @"/App/Merchantcomment/onthdoor";
            [self.navigationController pushViewController:pingjia animated:YES];
        }
        
    }else if (indexPath.section ==1) {
        DPZLViewController * dpzlvc =[[DPZLViewController alloc]init];
        dpzlvc.merid = self.getonModel.merchant.merchant_id;
        dpzlvc.merchanttype = self.merchanttype;
        [self.navigationController pushViewController:dpzlvc animated:YES];
    }else if (indexPath.section==3) {
        KFuViewController * vc =[[KFuViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

@end
